<?php

class Port extends Port_model {

    function __construct() {
        parent::__construct();
    }
    
    function getList($filter = array()) {
        $port = new Port();
        if(isset($filter['name']) && $filter['name']) {
            $port->addWhere("port.name LIKE '%".$filter['name']."%'");
        }
        $port->addJoin(new Country());
        $port->addWhere('port.is_deleted = 0');
        $port->addSelect();
        $port->addSelect("port.id, port.name, country.name as country_name");
        $port->find();
        
        return $port;            
    }
    
    function isDeleteAble($id) {
        $port = new Port();
        return true;
    }

    function validateInput() {

        $ci =& get_instance();
        $ci->load->helpers('utils');

        if (!$this->url || trim($this->url) == '') {
            MessageHandler::add ('Missing fan page URL');
        }

        if (!$this->name || trim($this->name) == '')
            MessageHandler::add ('Missing campaign name', MSG_ERROR, LOG_ONLY);

        if (!$this->click_target) {
            MessageHandler::add('Please choose Likes target for your campaign!');
        }
        else {
            $target = new OrderCoeff();
            $target->addSelect();
            $target->addSelect('id');
            $target->addWhere("target = '$this->click_target'");
            $target->addWhere("is_deleted = ".IS_NOT_DELETED);
            $target->addWhere("is_used = 1");
            $target->find();

            if (!$target->countRows() || $target->countRows() == 0) {
                MessageHandler::add('Sorry, now we cannot offer this target for you!');
            }
        }

        if (!$this->created_date)
            MessageHandler::add ('Missing campaign creation date', MSG_ERROR, LOG_ONLY);

        if (!$this->id_network)
            MessageHandler::add ('Please choose a network for this campaign', MSG_ERROR, LOG_ONLY);

        if (!$this->status)
            MessageHandler::add ('Missing campaign status');
        elseif (!in_array($this->status, array(CREATED, STARTED, PAUSED, CANCELED, COMPLETED)))
            MessageHandler::add ('Campaign status is invalid');

        if (!$this->start_date)
            MessageHandler::add ('Missing campaign start date');
        elseif (!isValidDate(trim($this->start_date))) {
            MessageHandler::add ('Start date is invalid.');
        }
        else {
            $this->start_date = date_to_date_sql($this->start_date);
            $start = date_sql_to_timestamp($this->start_date);
            $now = time();

            if ($now > $start) {
                MessageHandler::add ('Start date must be today or in the future.');
            }
        }

        return MessageHandler::countError() > 0 ? false : true;

    }
}
